import React, { useState } from 'react';
import { to_float, KInput, useKForm, WaitUmd, Btn, obj_str, loc, PanelFind, Ret, Title, ck_float, useCmd,
    is_empty, uJob, cline, useHigh, GridTable } from 'sui';
import { PsnView } from './psn';
import { DlgTask } from '../task/ctl';
import { qbc_col, qbc_task, qbc_data, dt_data } from 'away/calc/cmd';

const QbcView = ({ cur, hreturn, no_acc, mod }) => {
    const [open_num, openNum] = useState(false);
    const [main, setMain] = useState();
    const [high] = useHigh(200, 100);
    const umd = useCmd();
    const form = useKForm(null, {
        dmin: 0,
        dmax: 0,
    });

    if (umd.isFin("psn")) {
        return <PsnView cur={umd.cur} hreturn={umd.hreturn} tech={cur} mod={mod}/>;
    }

    const ck_empty_float = (v, k1) => {
        const kv = form.get(k1);
        if (!v && !kv) {
            return null;
        }

        return ck_float(v);
    };

    const hFind = () => {
        const ck = {
            standout: v => ck_empty_float(v, "standout"),
            dmin: v => ck_empty_float(v, "dmin"),
            dmax: v => {
                const e = ck_empty_float(v, "dmax");
                if (e) {
                    return e;
                }

                const min = parseFloat(form.get("dmin"));
                if (isNaN(min)) {
                    return loc("l_705");
                }

                const max = parseFloat(v);
                if (isNaN(max)) {
                    return loc("l_705");
                }

                if (min > max) {
                    return loc("l_705");
                }

                return null;
            }
        };

        if (form.isErr(ck)) {
            return;
        }

        umd.startWait();

        uJob(umd.endWait, `goods/${mod}`,
            {
                line: cline(),
                k: obj_str(cur, "sn"),
                standout: to_float(form.get("standout")),
                dmin: to_float(form.get("dmin")),
                dmax: to_float(form.get("dmax")),
            },
            r => {
                if (mod === "db" || mod === "qbc") {
                    qbc_data(r, setMain, mod);
                } else {
                    dt_data(r, setMain);
                }
            }, e => setMain(null));
    };

    const pact = {
        "task": v => {
            v["sn"] = `${obj_str(cur, "sn")}: ${obj_str(v, "groupA")} + ${obj_str(v, "groupB")}`;
            v["dnum"] = v.num;
            umd.setCur(v);
            openNum(true);
        },
    };

    const htask = (v) => {
        const val = qbc_task(v, umd, mod);
        if (is_empty(val)) {
            return;
        }

        umd.startWait();

        uJob(null, "goods/task",
            {
                line: cline(),
                k: obj_str(cur, "sn"),
                v: val,
            },
            r => hFind(),
            e => umd.setErr(), true);
    };

    const Right = () => (
        <div className="inline-flex items-center">
            <Ret className="ml-4" hcmd={hreturn}/>
        </div>
    );

    const Left = () => (
        <div className="inline-flex items-center">
            <Title mod={obj_str(cur, "sn")} sub={loc("l_887")}>QBC</Title>
        </div>
    );

    return (
        <div className="flex flex-col">
            <PanelFind left={<Left/>} right={<Right/>} hcmd={umd.setFilter}>
                <div className="inline-flex items-center mb-3">
                    <Btn className="mr-4" hcmd={() => hFind()} color="teal">{loc("l_832")}</Btn>
                    <div className="inline-flex items-center ml-4 px-2 border-blue-600 border">
                        <span className="text-base mr-4 mt-1">{`${loc("l_748")} ( b )`}</span>
                        <KInput label={loc("l_699")} form={form} name="standout" mg="dense" width="w-20"/>
                        <KInput label={loc("l_532")} form={form} name="dmin" mg="dense" width="w-24" className="mx-4"/>
                        <KInput label={loc("l_533")} form={form} name="dmax" mg="dense" width="w-24"/>
                    </div>
                    <Btn className="ml-4" hcmd={() => form.clear_all()}>{loc("l_543")}</Btn>
                </div>
            </PanelFind>
            <GridTable high={high} col={qbc_col(no_acc("na"), umd, pact, mod)} row={main}/>
            <DlgTask title={loc("l_350")} open={open_num} setOpen={openNum} hcmd={htask} data={umd.cur}/>
            <WaitUmd umd={umd}/>
        </div>
    );
};

export {QbcView};
